package com.example.administrator.uitest.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.uitest.R;
import com.example.administrator.uitest.intface.DoView;
import com.example.administrator.uitest.request.UploadRecord_Request;
import com.example.administrator.uitest.tool.UserTool;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/7/2 0002.
 */

public class Finish_Activity extends Activity {
    private TextView click_num;
    private SharedPreferences sharedPreferences;
    private TextView hit;
    private SharedPreferences.Editor editor;
    private Button btn_upload,btn_return;
    private int shootNum = 0;
    private int hitNum = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finish_activity);
        initView();
        Intent intent = getIntent();
        int num = intent.getIntExtra("num",0);
        shootNum = num;
        int hit_num = intent.getIntExtra("hit",0);
        hitNum = hit_num;
        click_num.setText(""+num);
        hit.setText(""+hit_num);

    }

    private void initView() {
        click_num = findViewById(R.id.click_num);
        hit = findViewById(R.id.hit);
        btn_return = findViewById(R.id.Return);
        btn_upload = findViewById(R.id.upload);
        btn_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                String time = simpleDateFormat.format(date);
                String params = "userId="+ UserTool.userId+"&time="+time+"&shoot="+shootNum+"&hit="+hitNum;
                UploadRecord_Request record_request = new UploadRecord_Request(Finish_Activity.this,params);
                record_request.conn(new DoView() {
                    @Override
                    public void setSuccessfulView(Object object) {
                        if(object.toString().equals("true")){
                            Toast.makeText(Finish_Activity.this, "上传成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(Finish_Activity.this,HomeActivity.class));
                            finish();
                        }else {
                            Toast.makeText(Finish_Activity.this, "上传失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void setFiledView(Object object) {
                        Toast.makeText(Finish_Activity.this, "上传失败,网络出错", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Finish_Activity.this,HomeActivity.class));
                finish();
            }
        });
    }
}
